#!/usr/bin/env python
# -*- coding: utf-8 -*-
from applications.extensions.db_plugin import db
from common.utils.helpers import now_beijing


class FlowmeterDailyStats(db.Model):
    """存储端口流量的每日统计数据"""
    __tablename__ = 'flowmeter_daily_stats'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键ID')
    port = db.Column(db.Integer, nullable=False, index=True, comment='端口号')  # 单列索引可以直接定义在字段上
    stat_date = db.Column(db.Date, nullable=False, index=True, comment='统计日期')  # 单列索引可以直接定义在字段上
    total_gb = db.Column(db.Numeric(precision=10, scale=4), nullable=False, default=0.0000, comment='总流量(GB)')
    total_pkts = db.Column(db.BigInteger, nullable=False, default=0, comment='总包数')
    total_bytes = db.Column(db.BigInteger, nullable=False, default=0, comment='总字节数')
    created_time = db.Column(
        db.DateTime, default=now_beijing,
        nullable=False, comment="创建时间"
    )
    updated_time = db.Column(
        db.DateTime, default=now_beijing, onupdate=now_beijing,
        nullable=False, comment="更新时间"
    )

    # 表级约束和索引必须定义在这里
    __table_args__ = (
        db.UniqueConstraint('port', 'stat_date', name='uk_port_stat_date'),  # 跨字段唯一约束
        db.Index('idx_port_stat_date', 'port', 'stat_date'),  # 跨字段复合索引
    )
